def max_subarray_sum(nums):
    max_current = max_global = nums[0]
    for i in range(1, len(nums)):
        max_current = max(nums[i], max_current + nums[i])
        if max_current > max_global:
            max_global = max_current
    return max_global
# 例如
arr = [1, -2, 3, 5, -1]
result = max_subarray_sum(arr)
print("子数组之和的最大值是:", result)
